<template>
    <template v-if="visible">
        <!--        //把它内部的元素都放到body下面 而不是在id='app'的div里面-->
        <Teleport to="body">
            <div class="dr-dialog-overlay" @click="closeOnClickOverlay"></div>
            <div class="dr-dialog-wrapper">
                <div class="dr-dialog">
                    <header>
                        <slot name="title"/>
                        <span @click="close" class="dr-dialog-close"></span>
                    </header>
                    <main>
                        <slot name="content"/>
                    </main>
                    <footer>
                        <Button @click="ok" theme="button">确认</Button>
                        <Button @click="cancel">取消</Button>
                    </footer>
                </div>
            </div>
        </Teleport>
    </template>
</template>

<script lang="ts">
    import Button from "../lib/Button.vue";

    export default {
        props: {
            visible: {
                type: Boolean,
                default: false
            },
            closeOnClickOverlay: {
                type: Boolean,
                default: false
            },
            ok: {
                type: Function,
            },
            cancel: {
                type: Function,
            }
        },
        name: "Dialog",
        components: {Button},
        setup(props, context) {
            const close = () => {
                context.emit('update:visible', false)
            }
            const closeOnClickOverlay = () => {
                if (props.closeOnClickOverlay) {
                    close()
                }
            }
            const ok = () => {
                // if(props.ok?.()!==false){
                if (props.ok && props.ok() !== false) {
                    close()
                }
            }
            const cancel = () => {
                props.cancel && props.cancel()
                // context.emit('cancel')
                close()
            }
            return {close, closeOnClickOverlay, ok, cancel}
        }
    }
</script>

<style lang="scss">
    $radius: 4px;
    $border-color: #e3e3e3;

    .dr-dialog {
        &-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: fade_out(black, 0.5);
            z-index: 10;
        }

        &-wrapper {
            position: fixed;
            left: 50%;
            top: 40%;
            transform: translate(-50%, -40%);
            z-index: 11;
        }

        background: white;
        border-radius: $radius;
        box-shadow: 0 0 3px fade_out(black, 0.5);
        min-width: 20em;
        max-width: 90%;

        > header {
            padding: 10px 16px;
            border-bottom: 1px solid $border-color;
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-size: 16px;
            font-weight: 700;
        }

        > main {
            padding: 12px 16px;
        }

        > footer {
            padding: 5px 10px;
            text-align: right;

            .dr-button:first-child {
                margin-right: 15px;
            }
        }

        &-close {
            position: relative;
            display: inline-block;
            width: 13px;
            height: 13px;
            cursor: pointer;

            &::before,
            &::after {
                content: '';
                position: absolute;
                height: 1px;
                background: #666;
                width: 100%;
                top: 50%;
                left: 50%;
            }

            &::before {
                transform: translate(-50%, -50%) rotate(-45deg);
            }

            &::after {
                transform: translate(-50%, -50%) rotate(45deg);
            }
        }
    }

</style>
